package fr.fermeManager.application.views;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import net.miginfocom.swing.MigLayout;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.swing.JRViewer;
import fr.fermeManager.application.controllers.ControllerRapport;
import fr.fermeManager.library.model.beans.Transaction;

public class ViewRapport extends BasePanel {

	List<Transaction> listeTransaction = new ArrayList<Transaction>();

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ControllerRapport currentController = new ControllerRapport();

	public ViewRapport(List<Transaction> listeFiltrees, String libelleFerme, Date dateDebut, Date dateFin) throws JRException {
		setMinimumSize(new Dimension(1000, 560));
		setPreferredSize(new Dimension(600, 560));

		JRDataSource datasource = new JRBeanCollectionDataSource(listeFiltrees);

		Map<String, Object> parameters = new HashMap<String, Object>();
		if (dateDebut != null)
			parameters.put("paramDateDebut", dateDebut);
		if (dateFin != null)
			parameters.put("paramDateFin", dateFin);
		parameters.put("paramLibelleFerme", libelleFerme);
		
		JasperReport report = (JasperReport) JRLoader.loadObject(getClass().getResource("/rapports/report4.jasper")); //$NON-NLS-1$

		 JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, datasource);
		setLayout(new MigLayout("", "[450px,grow]", "[177px,grow,fill][]"));

		this.add(new JRViewer(jasperPrint), "cell 0 0,grow");
		
		JButton btnRetour = new JButton("Retour");
		btnRetour.setIcon(new ImageIcon(ViewRapport.class.getResource("/img16x16/retour.png")));
		btnRetour.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				afficherGestionTransactions();
			}
		});
		add(btnRetour, "cell 0 1,alignx center,growy");
	}

	protected void afficherGestionTransactions() {
		currentController.afficherTransactions();
	}
}